<?php

namespace App\Services;

use App\Interfaces\MenuInterface;
use Illuminate\Support\Facades\DB;

class MenuService implements MenuInterface
{

    public function getUserMenu(array $arr): array
    {
        $res = DB::table('menus')->where('deleted',0)->whereIn('menuId',$arr)->orderBy('sortNumber','ASC')->get()->all();
        return $res;
    }

    public function getAllMenu(): array
    {
        $res = DB::table('menus')->where('deleted',0)->orderBy('sortNumber','ASC')->get()->all();
        return $res;
    }

    public function getMenuList(array $params): array
    {
        $where = array(['deleted',0]);
        if (!empty($params))
        {
            $where[] = ['title', 'like', '%' . $params['title'] . '%'];
        }
        if (!empty($params['path']))
        {
            $where[] = ['path', 'like', '%' . $params['path'] . '%'];
        }
        if (!empty($params['authority']))
        {
            $where[] = ['authority', 'like', '%' . $params['authority'] . '%'];
        }
        $res = DB::table('menus')->where($where)->orderBy('sortNumber','ASC')->get()->all();
        return $res;
    }

    public function updateMenu(mixed $data): int
    {
        $res = DB::table('menus')->where('menuId',$data['menuId'])->update($data);
        return $res;
    }

    public function createMenu(mixed $data): int
    {
        $res = DB::table('menus')->insertGetId($data);
        return $res;
    }
}
